package com.keta.foundation.service.impl;

import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.keta.foundation.dao.OrganizationDAO;
import com.keta.foundation.dao.OrganizationRoleDAO;
import com.keta.foundation.entity.Module;
import com.keta.foundation.entity.Organization;
import com.keta.foundation.entity.OrganizationRole;
import com.keta.foundation.entity.Role;
import com.keta.foundation.entity.User;
import com.keta.foundation.entity.UserRole;
import com.keta.foundation.exception.ExistedException;
import com.keta.foundation.exception.NotDeletedException;
import com.keta.foundation.exception.NotExistedException;
import com.keta.foundation.service.OrganizationRoleService;
import com.keta.foundation.service.OrganizationService;
import com.keta.foundation.service.UserService;
import com.keta.foundation.util.dwz.AjaxObject;
import com.keta.foundation.util.dwz.Page;
import com.keta.foundation.util.dwz.PageUtil;

@Service
@Transactional
public class OrganizationRoleServiceImpl implements OrganizationRoleService {

	@Autowired
	private OrganizationRoleDAO organizationRoleDAO;

	@Override
	public OrganizationRole get(Long id) {
		return organizationRoleDAO.findOne(id);
	}

	@Override
	public void save(OrganizationRole organizationRole) {
		organizationRoleDAO.save(organizationRole);
	}

	@Override
	public void delete(OrganizationRole organizationRole) {
		organizationRoleDAO.delete(organizationRole);
	}

	@Override
	public List<OrganizationRole> getOrganizationRole(Organization organization) {

		return organizationRoleDAO.findByOrganizationId(organization.getId());
	}

	@Override
	public void delByRole(Long roleId) {
		List<OrganizationRole> listOrganizationRole = organizationRoleDAO.findByRoleId(roleId);
		for (OrganizationRole organizationRole : listOrganizationRole) {
			organizationRoleDAO.delete(organizationRole);
		}
	}

	@Override
	public void delByOrganization(Long organizationId) {
		List<OrganizationRole> listOrganizationRole = organizationRoleDAO.findByOrganizationId(organizationId);
		for (OrganizationRole organizationRole : listOrganizationRole) {
			organizationRoleDAO.delete(organizationRole);
		}
	}

	@Override
	public void delByRole(Role role) {
		List<OrganizationRole> listOrganizationRole = organizationRoleDAO.findByRoleId(role.getId());
		for (OrganizationRole organizationRole : listOrganizationRole) {
			organizationRoleDAO.delete(organizationRole);
		}
	}

	@Override
	public void delByOrganization(Organization organization) {
		List<OrganizationRole> listOrganizationRole = organizationRoleDAO.findByOrganizationId(organization.getId());
		for (OrganizationRole organizationRole : listOrganizationRole) {
			organizationRoleDAO.delete(organizationRole);
		}
	}

}
